<template>
  <div class="table">
    <div class="header">
      <slot name="header">
        <div class="title">
          {{ title }}
        </div>
        <div class="handler">
          <slot name="handerHandler"></slot>
        </div>
      </slot>
    </div>

    <div class="content">
      <el-table
        :data="tableData"
        border
        height="100%"
        style="width: 100%"
        @selection-change="handleSelectChange"
      >
        <el-table-column
          v-if="showSelectColumn"
          type="selection"
          align="center"
          width="60px"
        >
        </el-table-column>
        <el-table-column
          v-if="showIndexColumn"
          type="index"
          label="序号"
          align="center"
          width="60px"
        ></el-table-column>
        <template v-for="(propItem, index) in propList" :key="index">
          <el-table-column v-bind="propItem">
            <template #default="scope">
              <slot :name="propItem.slotName" :row="scope.row">{{
                scope.row[propItem.prop]
              }}</slot>
            </template>
          </el-table-column>
        </template>
      </el-table>
    </div>

    <div class="footer">
      <slot name="footer">
        <el-pagination
          :page-sizes="[10, 20, 30, 40]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableCount"
          :current-page="page.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  props: {
    tableData: {
      type: Array,
      require: true
    },
    tableCount: {
      type: Number,
      default: 0
    },
    propList: {
      type: Array,
      require: true
    },
    showIndexColumn: {
      type: Boolean,
      default: () => false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    //
    page: {
      type: Object,
      default: () => ({ currentPage: 0, pageSize: 10 })
    }
  },
  emits: ['selectChange', 'update:page'],
  setup(props, { emit }) {
    const handleSelectChange = (value: any) => {
      emit('selectChange', value)
    }

    const handleCurrentChange = (currentPage: number) => {
      console.log(currentPage)
      emit('update:page', { ...props.page, currentPage })
    }
    const handleSizeChange = (pageSize: number) => {
      console.log(pageSize)
      emit('update:page', { ...props.page, pageSize })
    }

    return {
      handleSelectChange,
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style scoped lang="less">
.table {
  height: 100%;
  background: #fff;
  display: flex;
  flex-direction: column;
  .header {
    display: flex;
    height: 50px;
    padding: 0 10px;
    align-items: center;
    justify-content: space-between;
    .title {
      font-size: 20px;
      font-weight: bold;
    }
    .handler {
      align-items: center;
    }
  }
  .content {
    height: 0;
    flex: 1;
  }
  .footer {
    text-align: right;
    display: flex;
    align-items: center;
    height: 42px;
    padding: 0 10px;
    justify-content: flex-end;
  }
}
</style>
